package com.uenpay.util.crypt;

import com.uenpay.util.sun.misc.BASE64Decoder;
import com.uenpay.util.sun.misc.BASE64Encoder;

import java.io.IOException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SafeUrlCrypt {
    public static boolean initialized = false;


    public static String KEY = "vnw52dg7";
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
    private static final String DEFUALT_ENCODING = "UTF8";

    public static void main(String[] args) {
//        System.out.println("Hello World!");
        try {
            String result = decryptByAes("hDiu7plNv-Ej3G2RJHZn1iFIJ4tiWDmaT2upTPixcqvsH5TDyc8qaRSeK39Ugigm_TgIRFUqkgydJVJiONUyLvx87talYQXlT0aAJyqmS9I=", "vnw52dg7");
            System.out.println("descrypt=" + result);

            String encodeResult = encodeByAes("{\"custid\":\"21119\",\"nickname\":\"Liulou\",\"status\":\"1\",\"controller\":\"Cust\"}", "vnw52dg7");
            System.out.println("encode=" + encodeResult);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static SecretKeySpec getAesKey(String reqKey) {
        SecretKeySpec secretKeySpec;
        if (reqKey.length() < 16) {
            byte[] newkey = new byte[16];
            for (int i = 0; i < reqKey.getBytes().length; i++) {
                newkey[i] = reqKey.getBytes()[i];
            }
            secretKeySpec = new SecretKeySpec(newkey, "AES");
        } else {
            secretKeySpec = new SecretKeySpec(reqKey.getBytes(), "AES");
        }

        return secretKeySpec;
    }

    public static String encodeByAes(String sSrc, String reqKey) {
        SecretKeySpec secretKeySpec = getAesKey(reqKey);
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] original = cipher.doFinal(sSrc.getBytes("utf-8"));
            return safeUrlBase64Encode(original);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String decryptByAes(String sSrc, String reqKey) {
        try {
            SecretKeySpec secretKeySpec = getAesKey(reqKey);

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] encrypted1 = safeUrlBase64Decode(sSrc);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static String safeUrlBase64Encode(byte[] data) {
        String encodeBase64 = new BASE64Encoder().encode(data);
        String safeBase64Str = encodeBase64.replace('+', '-');
        safeBase64Str = safeBase64Str.replace('/', '_');
//        safeBase64Str = safeBase64Str.replaceAll("=", "");
        return safeBase64Str;
    }

    public static byte[] safeUrlBase64Decode(final String safeBase64Str) throws IOException {
        String base64Str = safeBase64Str.replace('-', '+');
        base64Str = base64Str.replace('_', '/');
        int mod4 = base64Str.length() % 4;
        if (mod4 > 0) {
            base64Str = base64Str + "====".substring(mod4);
        }
        System.out.println(base64Str);
        return new BASE64Decoder().decodeBuffer(base64Str);
    }
}

